package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.R
import com.bawei.module_home.adapter.HomeListAdapter.OnItemClickListener
import com.bawei.module_home.databinding.ItemVideoLayoutBinding
import com.bawei.module_home.databinding.VideoItemBinding
import com.bawei.module_home.entriy.SimpleVideoEntity
import com.blankj.utilcode.util.ToastUtils
import com.example.image.glide.GlideImgLoader

class HomePlayAdapter constructor(private val list:List<SimpleVideoEntity>):RecyclerView.Adapter<HomePlayAdapter.ItemViewHolder>() {

    private var itemClickListener: OnItemClickListener?= null

    fun setOnItemClickListener(l: OnItemClickListener){
        itemClickListener = l
    }

    interface OnItemClickListener{
        fun onItemChick(v: View, position: Int)
    }

    inner class ItemViewHolder(val dataBinding: VideoItemBinding): RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(VideoItemBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        holder.dataBinding.gsy.apply {
            setUp(entity.videopath,true,"")
        }
        GlideImgLoader.loadCircleImage(holder.dataBinding.root.context, entity.avatar_url, holder.dataBinding.playIcon)
        holder.dataBinding.playTitle.setText(entity.title)
        holder.dataBinding.playLovetv.setText(entity.playnum.toString())
        holder.dataBinding.playMessagetv.setText(entity.commentnum.toString())
        GlideImgLoader.loadCircleImage(holder.dataBinding.root.context, entity.avatar_url, holder.dataBinding.playUsericon)


        holder.dataBinding.playTitle.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        holder.dataBinding.playLove.setOnClickListener {
            entity.islike=!entity.islike
            if(entity.islike){
                holder.dataBinding.playLove.setImageResource(R.drawable.playlove2)
                ToastUtils.showShort("点赞成功")
            }else{
                holder.dataBinding.playLove.setImageResource(R.drawable.playllovve)
                ToastUtils.showShort("取消点赞")
            }
            notifyItemChanged(position)
            itemClickListener?.onItemChick(it, position)
        }

        holder.dataBinding.playMessage.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        holder.dataBinding.playSc.setOnClickListener {
            entity.issc=!entity.issc
            if(entity.issc){
                holder.dataBinding.playSc.setImageResource(R.drawable.playsccc)
                ToastUtils.showShort("收藏成功")
            }else{
                holder.dataBinding.playSc.setImageResource(R.drawable.playsc222)
                ToastUtils.showShort("取消收藏")
            }
            notifyItemChanged(position)
            itemClickListener?.onItemChick(it, position)
        }

    }
}